#include "client.h"
#include "ui_client.h"
#include <QSize>
#include <QMovie>
#include <QPixmap>
#include <QImage>
//#include <qcheckbox.h>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QListWidget>
#include"item/label_button.h"
#include <QVector>

#include<QLabel>

client::client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::client)
{
    ui->setupUi(this);
    list=new QListWidget(this);
    list->setGeometry(60,80,900,900);

    this->setMeal=nullptr;
   //QTableWidget *table=new QTableWidget(this);
//    table1=new QTableWidget(this);
//    table1->setGeometry(60,100,800,240);
//    table1->setWindowTitle("菜单1");
//    table2=new QTableWidget(this);
//    table2->setGeometry(60,400,800,240);
//    table2->setWindowTitle("菜单2");
    //init(table2);
}

//获得指定套餐的各个菜单数组
//nums(21) 应该插入 menu 332 ！
QVector<int> Menus;
//for(int i=0;i< this->nums.size();i++){
//    for(int j=0;j<this->nums[i];j++)
//        Menus.push_back(this->foodTypes[i].foods.size());
//}


//获得菜单的菜品数
QVector<int> ::iterator cou=Menus.begin();

//获得菜品种数
int count_table=Menus.size();


int client::init()
{
    // cccc
//    for(int i=0;i< this->nums.size();i++){
//        for(int j=0;j<this->nums[i];j++)
//            Menus.push_back(this->foodTypes[i].foods.size());
//    }

//    list->clear();
//    for (int cln_list=0;cln_list<count_table ; cln_list++) {
//        QTableWidget *table=new QTableWidget(this);
//        table->clear();
//        table->setRowCount(3);
//        table->setRowHeight(0,130);
//        table->setColumnWidth(cln_list,210);
//        table->setRowHeight(1,50);
//        table->setRowHeight(2,50);
//        table->horizontalHeader()->hide();//取消行号

//        //table->setColumnCount(count);
//        table->setColumnCount(1+Menus[cln_list]);
//        //

//        for(int clmnIndex=1;clmnIndex<(Menus[cln_list])+1;clmnIndex++)
//        {
//            //QTableWidgetItem *item_index = new QTableWidgetItem (QString::number(clmnIndex+1));
//            //table->setItem(1,clmnIndex,item_index);
//            //QString str = "添加" + QString::number(clmnIndex+1,cln_list=1);
//            QString str = "添加";

//            //图片Label
//            QLabel *lb=new QLabel(this);
//            //在这个label上添加图片
//            QPixmap pixmap(":/new/prefix1/shitang.jpg");
//            pixmap=pixmap.scaled(120,120,Qt::KeepAspectRatio,Qt::SmoothTransformation);
//            lb->setPixmap(QPixmap(pixmap));

//            //菜名Label
//            QLabel *lb1=new QLabel(this);
//            //在这个Label上添加名字
//            lb1->setText("菜名");

//            //按钮设置
//            Label_button * bt=new Label_button(this);
//            bt->column=clmnIndex;
//            bt->row=cln_list;
//            bt->setText(str);
//            bt->setFixedSize(70,40);
//            bt->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
//                              "QPushButton:pressed{background-color:rgb(51,129,172)}");

//            //在第一列放置提示信息
//            //菜单类型
//            QString str1= "菜单类型";
//            QTableWidgetItem *item1 = new QTableWidgetItem (str1);
//            item1->setFont(QFont("SimHei",13));
//            table->setItem(0,0,item1);
//            table->item(0,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

//            //菜品份数：
//            QString str2= "菜品份数";
//            QTableWidgetItem *item2 = new QTableWidgetItem (str2);
//            item2->setFont(QFont("SimHei",13));
//            table->setItem(1,0,item2);
//            table->item(1,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

//            //餐品数量
//            int num= 2;
//            QTableWidgetItem *item3 = new QTableWidgetItem (QString::number(num));//int型要转成QString才能显示
//            item3->setFont(QFont("SimHei",13));
//            table->setItem(2,0,item3);
//            table->item(2,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);


//            //连接信号槽
//            connect(bt,SIGNAL(clicked()),this,SLOT(addOrder()));
//            //水平布局，在每个第0行表格中放置一个图片
//            QHBoxLayout *vImge=new QHBoxLayout();
//            QWidget *Widget_lb=new QWidget();
//            vImge->addWidget(lb);
//            Widget_lb->setLayout(vImge);
//            table->setCellWidget(0,clmnIndex,Widget_lb);

//            //水平布局，在第2行每个表格里菜名
//            QHBoxLayout *vName=new QHBoxLayout();
//            QWidget *Widget_lb1=new QWidget();
//            vName->addWidget(lb1);
//            Widget_lb1->setLayout(vName);
//            table->setCellWidget(1,clmnIndex,Widget_lb1);

//            //水平布局,在第3行每个表格里放一个按钮
//            QHBoxLayout *vLayout=new QHBoxLayout();
//            QWidget *Widget_bt=new QWidget;
//            vLayout->addWidget(bt);
//            Widget_bt->setLayout(vLayout);
//            //Widget_bt->setStyleSheet("QWidget{ border: 0px }");
//            table->setCellWidget(2,clmnIndex,Widget_bt);
//            table->verticalHeader()->setHidden(true);
//        }
//        QListWidgetItem *item_table=new QListWidgetItem;
//        QSize size=item_table->sizeHint();//item尺寸
//        item_table->setSizeHint(QSize(size.width(),300));
//        list->addItem(item_table);
//        table->setSizeIncrement(size.width(),100);
//        list->setItemWidget(item_table,table);
//    }
//    Menus.clear();//清空容器

    int cln_list=0;

    for(unsigned int i=0;i<setMeal->nums.size();i++)
    {
        vector<SaleBase*> _vct_sales=setMeal->foodTypes[i].foods;
//        for(int k=0;k<setMeal->nums[i];k++)//
//        {

            QTableWidget *table=new QTableWidget(this);
            table->clear();
            table->setRowCount(3);
            table->setRowHeight(0,130);
            table->setColumnWidth(cln_list,210);
            table->setRowHeight(1,50);
            table->setRowHeight(2,50);
            table->horizontalHeader()->hide();//取消行号

            //设置列数，一类菜的数量
            unsigned int colNum=_vct_sales.size()+1;
            table->setColumnCount(colNum);

            for(unsigned int clmnIndex=1;clmnIndex<colNum;clmnIndex++)
            {
                //QTableWidgetItem *item_index = new QTableWidgetItem (QString::number(clmnIndex+1));
                //table->setItem(1,clmnIndex,item_index);
                //QString str = "添加" + QString::number(clmnIndex+1,cln_list=1);
                QString str = "添加";

                //图片Label
                QLabel *lb=new QLabel(this);
                //在这个label上添加图片
                QPixmap pixmap=_vct_sales[clmnIndex-1]->image;
                pixmap=pixmap.scaled(120,120,Qt::KeepAspectRatio,Qt::SmoothTransformation);
                lb->setPixmap(QPixmap(pixmap));

                //菜名Label
                QLabel *lb1=new QLabel(this);
                //在这个Label上添加名字
                lb1->setText(QString::fromStdString(_vct_sales[clmnIndex-1]->name));

                //按钮设置
                Label_button * bt=new Label_button(this);
                bt->column=clmnIndex;
                bt->row=cln_list;
                bt->setText(str);
                bt->setFixedSize(70,40);
                bt->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
                                  "QPushButton:pressed{background-color:rgb(51,129,172)}");

                //在第一列放置提示信息
                //菜单类型
                QString str1= QString::fromStdString(setMeal->foodTypes[i].name);
                QTableWidgetItem *item1 = new QTableWidgetItem (str1);
                item1->setFont(QFont("SimHei",13));
                table->setItem(0,0,item1);
                table->item(0,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

                //菜品份数：
                QString str2= "菜品份数";
                QTableWidgetItem *item2 = new QTableWidgetItem (str2);
                item2->setFont(QFont("SimHei",13));
                table->setItem(1,0,item2);
                table->item(1,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

                //餐品数量
                int num= setMeal->nums[i];
                QTableWidgetItem *item3 = new QTableWidgetItem (QString::number(num));//int型要转成QString才能显示
                item3->setFont(QFont("SimHei",13));
                table->setItem(2,0,item3);
                table->item(2,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);


                //连接信号槽
                connect(bt,SIGNAL(clicked()),this,SLOT(addOrder()));
                //水平布局，在每个第0行表格中放置一个图片
                QHBoxLayout *vImge=new QHBoxLayout();
                QWidget *Widget_lb=new QWidget();
                vImge->addWidget(lb);
                Widget_lb->setLayout(vImge);
                table->setCellWidget(0,clmnIndex,Widget_lb);

                //水平布局，在第2行每个表格里菜名
                QHBoxLayout *vName=new QHBoxLayout();
                QWidget *Widget_lb1=new QWidget();
                vName->addWidget(lb1);
                Widget_lb1->setLayout(vName);
                table->setCellWidget(1,clmnIndex,Widget_lb1);

                //水平布局,在第3行每个表格里放一个按钮
                QHBoxLayout *vLayout=new QHBoxLayout();
                QWidget *Widget_bt=new QWidget;
                vLayout->addWidget(bt);
                Widget_bt->setLayout(vLayout);
                //Widget_bt->setStyleSheet("QWidget{ border: 0px }");
                table->setCellWidget(2,clmnIndex,Widget_bt);
                table->verticalHeader()->setHidden(true);
            }
            QListWidgetItem *item_table=new QListWidgetItem;
            QSize size=item_table->sizeHint();//item尺寸
            item_table->setSizeHint(QSize(size.width(),300));
            list->addItem(item_table);
            table->setSizeIncrement(size.width(),100);
            list->setItemWidget(item_table,table);
//        }

        cln_list++;
    }

    return 0;
}



client::~client()
{
    delete ui;
}

void client::addOrder()
{

    QPushButton *pbt=dynamic_cast<QPushButton*>(this->sender());
    if(NULL==pbt)
    {
        return;
    }
    //显示菜品
    Label_button* pushButton=(Label_button*)pbt;
    myOrder myorder;
    myorder.x=pushButton->row;
    myorder.y=pushButton->column;
    myorders.push_back(myorder);
    qDebug()<<"菜单"<<pushButton->row<<" "<<"菜品"<<pushButton->column;
    if(aviliableNum[pushButton->row]<=0)
        return;
    aviliableNum[pushButton->row]-=1;
    QTableWidget *table=(QTableWidget *)list->itemWidget(list->item(pushButton->row));
    table->item(2,0)->setText(QString::number(aviliableNum[pushButton->row]));
    //添加到套餐中
    setMeal->addSelectFood(setMeal->foodTypes[pushButton->row].foods[pushButton->column-1]);
    //qDebug()<<"行id："<<pushButton->row;
    //qDebug()<<"列id："<<pushButton->text();


//    qDebug()<<pushButton->text();
//    //获取按钮的x/y坐标
//    int x=pushButton->parentWidget()->pos().x();
//    int y=pushButton->parentWidget()->pos().y();
//    qDebug()<<"x="<<x<<",y="<<y;
//    //根据按钮的xy坐标定位对应单元格
//    QModelIndex index=table->indexAt(QPoint(x,y));
//    //获取该按钮所在表格的行号和列号
//    int row=index.row();
//    int column=index.column();
//    //获得第二行的信息
//    qDebug()<<"行id："<<table->item(row,column)->text();
}



void client::on_pushButton_clicked()
{
//    int n=myorders.size();
//    for (int i=0;i<n ;i++ ) {
//        qDebug()<<"菜单"<<myorders[i].x<<myorders[i].y;
//    }
//    myorders.clear();
    this->close();
}
